package com.ffx.web.servlet;



import com.ffx.pojo.Student;
import com.ffx.service.StudentService;
import com.ffx.service.impl.StudentServiceImpl;
import com.mysql.cj.util.StringUtils;


import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentService studentService;
    @Override
    public void init() throws ServletException {
        studentService = new StudentServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String cmd = req.getParameter("cmd");
        if (StringUtils.isNullOrEmpty(cmd)){
            if ("list".equals(cmd)){
                selectStudentList(req,resp);
            }
        }
    }

    private void selectStudentList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询所有学生
        List<Student> listStudent =  studentService.findAll();
        //将学生列表放入请求作用域中
        req.setAttribute("list",listStudent);
        //进行页面的转发
        req.getRequestDispatcher("/WEB-INF/student/list.jsp").forward(req,resp);
    }
}
